package com.freework.util.excel.read.vo;
import javax.xml.bind.annotation.adapters.XmlAdapter;

import com.freework.util.SpringBeanUtil;
import com.freework.util.excel.read.service.rowToObject.ValueFormat;
public class ValueFormatAdapter extends XmlAdapter<String, ValueFormat> {

	@Override
	public String marshal(ValueFormat v) throws Exception {
		if (v == null)
			return null;
		return v.getClass().getName();
	}
	@Override
	public ValueFormat unmarshal(String v) throws Exception {
		if (v == null || v.equals(""))
			return null;
		int index=v.indexOf(":");
		boolean isSpringBean=false;
		if(index>=0){
			isSpringBean=v.substring(0, index).equals("beanid");
			v=v.substring(index+1);
		}
		if(isSpringBean){
			return (ValueFormat) SpringBeanUtil.getBean(v);
		}else{
			Class clazz=Class.forName(v);
			if(ValueFormat.class.isAssignableFrom(clazz))
				return (ValueFormat) Class.forName(v).newInstance();
			else
				return null;
		}
	}

}
